package com.fastloan.mgt.manager.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.model.ZyCustomerCard;
import com.fastloan.mgt.daily.service.WCustomerManagerDailyService;
import com.fastloan.mgt.manager.dao.ManagerMapper;
import com.fastloan.mgt.manager.dao.UserCardMapper;
import com.fastloan.mgt.manager.model.Manager;
import com.fastloan.mgt.manager.model.UserCard;
import com.fastloan.mgt.manager.service.ManagerService;
import com.fastloan.mgt.verify.model.VerifyResult;
import com.fastloan.mgt.verify.model.bankcard.BankCardVerifyParam;
import com.fastloan.mgt.verify.model.bankcard.BankCardVerifyResult;
import com.fastloan.mgt.verify.service.VerifyService;
import com.fastloan.safe.core.param.RoleTypeEnum;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.org.model.CmuOrg;
import com.fastloan.safe.mgt.org.model.vo.OrgTreeVo;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.model.vo.CmuUserVo;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

/**
 * @author admins
 * createTime 2017/11/16
 */
@Service
public class ManagerServiceImpl extends BasicServiceImpl<Manager> implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private CmuUserService cmuUserService;

    @Autowired
    private CmuOrgService cmuOrgService;

    @Autowired
    private UserCardMapper userCardMapper;

    @Autowired
    private VerifyService verifyService;

    @Override
    public Integer insertList(List<Manager> managerList) {
        return managerMapper.insertList(managerList);
    }


    @Override
    @DataTable(name = "queryManagerAll")
    public List<CmuUserVo> queryManagerAll(Map<String, Object> map) {
        List<OrgTreeVo> orgTreeVos = cmuOrgService.selectSonByTop(getOrgIdByAdminOrOther(), true, false);
        List<String> orgIds = orgTreeVos.stream().map(OrgTreeVo::getId).collect(toList());
        map.put("orgIds", orgIds);
        return managerMapper.queryManagerTemp(map);
    }

    @Override
    public ResultTo bindCard(String cardNum, String phone, String userId){
        CmuOrg cmuOrg = cmuOrgService.selectByPrimaryKey(ManagerKit.getTopOrgId());
        if (!cmuOrg.getOrgType().equals(1)){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"机构类型错误！");
        }
        CmuUser cmuUser = cmuUserService.selectByPrimaryKey(userId);
        BankCardVerifyResult bankCardVerifyResult = verifyService
                .verifyBankCard(BankCardVerifyParam.BankCardVerifyParamBuilder.aBankCardVerifyParam()
                        .withBankCardNo(cardNum).withIdentity(cmuUser.getIdCard()).withName(cmuUser.getCname())
                        .withPhone(phone).build());
        if (!bankCardVerifyResult.allAccord()){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"验证未通过!");
        }
        UserCard userCard = new UserCard(userId,cardNum);
        if (userCardMapper.selectCount(userCard) != 0){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"该银行卡已绑定!");
        }
        userCardMapper.insert(userCard);
        return new ResultTo();
    }

    @Override
    public List<UserCard> getUserCard(String userId){
        return userCardMapper.selectUserCard(userId);
    }

    @Override
    public int deleteCard(UserCard userCard) {
        return userCardMapper.delete(userCard);
    }
}
